<template>
	<view class="page flex-column">
		<view class="topVue">
			<view class="safeVue" :style="{ height: statusBarHeight + 'rpx' }"></view>
			<view class="flex align-center padding-row30" :style="{ height: barHeight + 'rpx', top: statusBarHeight + 'rpx' }" @click="$tools.back()">
				<image src="../../static/back.png" style="width: 48rpx; height: 48rpx;"></image>
				<text style="line-height: 1; color: #454545; font-weight: 600; font-size: 32rpx; padding-left: 20rpx;">邻工服务</text>
			</view>
		</view>
		
		<view :style="{ height: statusBarHeight + barHeight + 'rpx' }"></view>
		
		<view class="padding-row30">
			<u-subsection :list="tabs" :current="current" button-color="#D37430" active-color="#fff" @change="change"></u-subsection>
		</view>
		
		<template v-if="current==0">
			<view class="box margin20 flex align-center padding-row30" @click="setState()">
				<view class="label" style="padding: 0 30rpx 0 0; line-height: 1;">邻工接单服务开关</view>
				<image src="../../static/mine/switched.png" style="width: 80rpx; height: 80rpx;" v-if="checked"></image>
				<image src="../../static/mine/switch.png" style="width: 80rpx; height: 80rpx;" v-else></image>
			</view>
			<view class="box margin20 padding40" v-for="(item, index) in list" :key="index">
				<view class="label" style="padding-top: 0;">店铺名称：{{item.shopName}}</view>
				<view class="label">{{item.homecareName}}</view>
				<view class="label">联系方式：{{item.phoneNo}}</view>
				<view class="label">上门时间：{{item.createTime || "无"}}</view>
				<view class="label">发表时间：{{item.visitTime}}</view>
				<view class="label">预付定金：{{item.deposit}}元</view>
				<view class="label">地区：{{item.area}}</view>
				<view class="label">地址：{{item.address}}</view>
				<view class="label">说明情况: {{item.description || "无"}}</view>
				<view class="btn margin50" @click="submit(item.id)">抢单服务</view>
			</view>
		</template>
		
		<template v-if="current==1">
			<view class="box margin20 padding40" v-for="(item, index) in list" :key="index">
				<view class="label" style="padding-top: 0;">店铺名称：{{item.shopName}}</view>
				<view class="label">{{item.homecareName}}</view>
				<view class="label flex align-center">联系方式：{{item.phoneNo}}  <view class="btn1" @click="phoneCall(item.phoneNo)">拨打电话</view> </view>
				<view class="label">上门时间：{{item.createTime || "无"}}</view>
				<view class="label">发表时间：{{item.visitTime}}</view>
				<view class="label">预付定金：{{item.deposit}}元</view>
				<view class="label">地区：{{item.area}}</view>
				<view class="label">地址：{{item.address}}</view>
				<view class="label">说明情况: {{item.description || "无"}}</view>
				<view class="btn margin50" @click="$tools.route('./ing' ,item.id)">去完成</view>
			</view>
		</template>
		
		<template v-if="current==2">
			<view class="box margin20 padding40" v-for="(item, index) in list" :key="index">
				<view class="label" style="padding-top: 0;">店铺名称：{{item.shopName}}</view>
				<view class="label">{{item.homecareName}}</view>
				<view class="label">联系方式：{{item.phoneNo}}</view>
				<view class="label">上门时间：{{item.createTime || "无"}}</view>
				<view class="label">发表时间：{{item.visitTime}}</view>
				<view class="label">预付定金：{{item.deposit}}元</view>
				<view class="label">地区：{{item.area}}</view>
				<view class="label">地址：{{item.address}}</view>
				<view class="label">说明情况: {{item.description || "无"}}</view>
			</view>
		</template>
	
		
	
	</view>
</template>

<script>
	import { mapState } from "vuex"
	export default {
		data(){
			return {
				statusBarHeight: 0,
				barHeight: 0,
				tabs: [{name: "新任务"}, {name: "接单明细"}, {name: "已完成"}],
				current: 0,
				checked: false,
				list: [],
				
			}
		},
		
		computed: {
			...mapState({
				user: state=> state.user
			})
		},
		
		onLoad() {
			let that = this;
			wx.getSystemInfo({
				success(res) {
					let deviceWidth = wx.getSystemInfoSync().windowWidth
					that.statusBarHeight = Number(res.statusBarHeight) * ( 750 / deviceWidth ) 
					let menuButtonInfo = wx.getMenuButtonBoundingClientRect()
					let barHeight = menuButtonInfo.height + (menuButtonInfo.top - res.statusBarHeight) * 2
					that.barHeight = Number(barHeight) * ( 750 / deviceWidth )
				},
			});
		},
		
		async onShow() {
			await this.getList()
		},
		
		methods:{
			switchChange(e){
				this.checked = e
			},
			change(e){
				this.current = e
				if(e==0) this.getList()
				if(e==1 || e==2) {
					this.list = []
					this.getDetail()
				}
			},
			
			phoneCall(phone){
				uni.makePhoneCall({
					phoneNumber: phone //仅为示例
				});
			},
			
			async getDetail(){
				const res = await this.$request(`/api/WeChat/homecareWorkerOrderList?state=${this.current}`)
				if(res.code==200) {
					this.list = res.data
				}
			},
			
			// 抢单
			async submit(id){
				console.log(id)
				const res = await this.$request(`/api/WeChat/homecareWorkerOrder?workerId=${this.user.id}&orderId=${id}`, {
				}, "POST")
				if(res.code==200) {
					console.log(res)
					this.$u.toast("抢单成功")
					this.getList()
				}else{
					this.$u.toast(res.msg)
				}
			},
			
			// 开关
			async setState(){
				const res = await this.$request(`/api/WeChat/changeWorkerState?userId=${this.user.id}&state=${this.checked ? 1 : 0}`, {}, "POST")
				if(res.code==200) {
					this.$u.toast("操作成功")
					this.getList()
				}
			},
			
			// 获取列表
			async getList(){
				const res = await this.$request("/api/WeChat/homecareOrderList", {
					userId: this.user.id
				}, "GET")
				if(res.code==200) {
					this.list = res.data.list
					this.checked = res.data.orderState===0? true : false
					console.log("服务列表")
				}
			}
		}
	}
</script>

<style lang="less" scoped>
	.topVue {
		width: 100%;
		background: #fff;
		position: fixed;
		left: 0;
		top: 0;
		z-index: 99;
		.safeVue {
			width: 100%;
		}
	}

	.conVue {
		width: 690rpx;
		.li {
			width: 330rpx;
			height: 330rpx;
			border-radius: 20rpx;
			box-shadow: 0rpx 0rpx 16rpx 0rpx rgba(178, 178, 178, 0.25), inset 0rpx 8rpx 8rpx 0rpx rgba(255, 255, 255, 0.25);
			position: relative;
			margin-top: 20rpx;
		}
	}
	
	textarea {
		width: 690rpx;
		height: 200rpx;
		background: #f5f5f5;
		border-radius: 15rpx;
		display: block;
		box-sizing: border-box;
		padding: 20rpx;
		color: #333;
		margin: 20rpx auto 0;
	}
	
	

	.box {
		width: 690rpx;
		min-height: 88rpx;
		border-radius: 20rpx;
		box-shadow: 0rpx 0rpx 16rpx 0rpx rgba(178, 178, 178, 0.25), inset 0rpx 8rpx 8rpx 0rpx rgba(255, 255, 255, 0.25);
		position: relative;
		.label {
			color: #454545;
			line-height: 1;
			font-size: 30rpx;
			padding-top: 25rpx;
			.btn1 {
				width: 150rpx;
				height: 66rpx;
				background: #D37430;
				border-radius: 10rpx;
				line-height: 66rpx;
				text-align: center;
				color: #fff;
				margin-left: 30rpx;
			}
		}
		.btn {
			width: 520rpx;
			height: 88rpx;
			background: #D37430;
			border-radius: 12rpx;
			line-height: 88rpx;
			text-align: center;
			color: #fff;
		}
	}
	
	
</style>